﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace RoketPack.Management.Objects
{
    class Folder : Object
    {
        private string p_Local = null;

        internal Folder(string local)
        {
            this.p_Local = local;
        }

        internal string Local
        {
            get
            {
                return this.p_Local;
            }
        }

        internal override bool Download(Installation installation, Manager.VoidLambdaDouble progressevent)
        {
            return false;
        }

        internal override bool DownloadRequired(Installation installation)
        {
            return false;
        }

        internal override bool Install(Installation installation)
        {
            try
            {
                if (System.IO.Directory.Exists(PathTranslator.Parse(this.p_Local, this.p_Variables)))
                    return true;

                System.IO.Directory.CreateDirectory(PathTranslator.Parse(this.p_Local, this.p_Variables));
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        internal override bool Update(Installation installation)
        {
            // Handle deletion.
            if (this.DeleteOnNextUpdate)
            {
                return this.Uninstall(installation);
            }

            try
            {
                if (System.IO.Directory.Exists(PathTranslator.Parse(this.p_Local, this.p_Variables)))
                    return true;

                System.IO.Directory.CreateDirectory(PathTranslator.Parse(this.p_Local, this.p_Variables));
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        internal override bool Uninstall(Installation installation)
        {
            try
            {
                System.IO.Directory.Delete(PathTranslator.Parse(this.p_Local, this.p_Variables));
                return true;
            }
            catch (DirectoryNotFoundException)
            {
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}
